import axios from 'axios';

// 创建axios实例
const service = axios.create({
  timeout: 15000, // 请求超时时间
  headers: {}
});

// request拦截器
service.interceptors.request.use(
  config => {
    if (config.method === 'get' || config.method === 'put') {
      config.params = config.data;
    }
    return config;
  },
  error => {
    // Do something with request error
    Promise.reject(error);
  }
);

// response 拦截器
service.interceptors.response.use(
  response => {
    const res = response;
    if (res.status !== 200) {
      //接口是否请求成功
      return Promise.reject('error');
    } else {
      if (res.headers['content-type'] == 'application/x-xls') {
        return response;
      }
      return response.data;
    }
  },
  error => {
    if (error.message.includes('timeout')) {
      // 判断请求异常信息中是否含有超时timeout字符串
      return Promise.reject(error); // reject这个错误信息
    } else {
      return Promise.reject(error);
    }
  }
);

export default service;
